# !/user/bin/env python3
# -*- coding: utf-8 -*-
# -- anth bluedolphi ---
import datetime
import json
import time

import requests
from borax.calendars import LunarDate
from borax.calendars.lunardate import LCalendars

from spider.AIYZ.AIYOUZHANSpider import AIYZSpider
from spider.AIYZ.AIYZAdminSpider import AIYZAdminSpider


def create_coupon(spider):
    const = 5
    step = 5
    while const < 200:
        const += step
        name = "%s元超市券" % const
        type = 2
        id = spider.get__coupon_template_id(name, type)
        if id is not None:
            print("已经添加")
            # spider.del_coupon_template_by_id(id)
            continue
        payload = {"id": "", "style": "green-青春绿", "name": name, "type": "2", "minReduce": const,
                   "description": "", "token": spider.token}
        spider.create_coupon_template_by_payload(payload)

        time.sleep(1)

def create_shougon_activity(spider):
    const = 0
    step = 5
    maxConst = 200
    while const < maxConst:
        const += step
        activity_name = "手推 %s元超市券" % const
        activity_id = spider.get_activity_id(activity_name, type=3)
        print("activity_id=", activity_id)
        if activity_id is None:
            payload = {"addActivity_payMethod": "5", "couponStartTime": "", "couponEndTime": "", "expiry": "2",
                       "activeDay": "90",
                       "delayDay": "0", "given": "1", "mutualUse": "0", "maxPerDay": "", "maxPerUser": "",
                       "limitTimes": "",
                       "limitDays": "", "limitWeeks": "", "memberLevel": "", "allowPayMethods": "1,2,4,5", "id": "",
                       "name": activity_name, "type": "3", "amountType": "1", "minAmount": "", "maxAmount": "",
                       "minArriveAmount": "", "maxArriveAmount": "", "giveOilType": "", "giveOilTypeNames": "",
                       "needMember": "0",
                       "startTime": "2020-06-01 00:00:00", "endTime": "2020-06-30 23:59:59",
                       "oilTypeIds": "1266201205005860865",
                       "oilTypeNames": "超市", "description": "", "limitDaysJoinTimes": "", "limitWeeksJoinTimes": "",
                       "limitMonthJoinTimes": "", "limitJoinTimes": "", "token": spider.token}
            activity_id = spider.create_activity_by_payload(payload)

        template_name = "%s元超市券" % const
        type = 2  # 满减券

        templateId = spider.get__coupon_template_id(template_name, type)
        if spider.is_activity_has_coupon_template_id(activity_id, templateId) is False:
            if templateId is None:
                print("获取优惠券模板失败！")
            else:
                payload = {"id": "", "templateId": templateId, "couponCost": const,
                           "description": "超市购物使用满%s元可以使用！" % const, "quantity": "1",
                           "allowGiveAway": "", "maxActivePerUser": "", "activityId": activity_id,
                           "token": spider.token}
                spider.add_coupon_to_activity_by_payload(payload)

        time.sleep(1)

def qiyou_activity(spider):
    const = 200
    step = 50
    per = 0.1
    maxConst = 700
    while True:
        const += step
        activity_name = "加汽油%s返%s元超市全额券" % (const, int(const * per))
        activity_id = spider.get_activity_id(activity_name, type=5)
        print("activity_id=", activity_id)
        if activity_id is None:
            payload = {"addActivity_payMethod": "5", "couponStartTime": "", "couponEndTime": "", "expiry": "2",
                       "activeDay": "60", "delayDay": "0", "given": "1", "mutualUse": "0", "maxPerDay": "",
                       "maxPerUser": "", "limitTimes": "", "limitDays": "", "limitWeeks": "", "memberLevel": "",
                       "allowPayMethods": "1,2,4,5", "id": "", "name": activity_name,
                       "type": "5", "amountType": "1", "minAmount": const, "maxAmount": const + step - 0.01,
                       "minArriveAmount": "",
                       "maxArriveAmount": "", "giveOilType": "1250709476189700098,1250709515796512770",
                       "giveOilTypeNames": "92#,95#", "needMember": "1", "startTime": "2020-06-01 00:00:00",
                       "endTime": "2021-05-26 23:59:59", "oilTypeIds": "1265170483226853377", "oilTypeNames": "超市",
                       "description": "", "limitDaysJoinTimes": "", "limitWeeksJoinTimes": "",
                       "limitMonthJoinTimes": "", "limitJoinTimes": "", "token": spider.token}
            activity_id = spider.create_activity_by_payload(payload)

        template_name = "%s元超市券" % int(const * per)
        type = 3  # 加油返券

        templateId = spider.get__coupon_template_id(template_name, type)
        if spider.is_activity_has_coupon_template_id(activity_id, templateId) is False:
            if templateId is None:
                print("获取优惠券模板失败！")
            else:
                payload = {"id": "", "templateId": templateId, "couponCost": int(const * per),
                           "description": "代金券全额使用，烟酒不参与活动，有效期内抵用！", "quantity": "1", "allowGiveAway": "",
                           "maxActivePerUser": "", "activityId": activity_id, "token": spider.token}

                spider.add_coupon_to_activity_by_payload(payload)

        if const >= maxConst:
            break;
        time.sleep(1)


# 计算闰月
def is_leap_month():
    days = [8, 18, 28]
    month = 4
    limitDays = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
                 29, 30, 31]

    limitDays.remove(20)
    limitDays.remove(30)
    str1 = ''.join(str(i) + "," for i in limitDays)
    str1 = str1[:-1]

    months_limitDays = {
        '1': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
              30, 31],
        '2': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
              30, 31],
        '3': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
              30, 31],
        '4': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
              30, 31],
        '5': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
              30, 31],
        '6': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
              30, 31],
        '7': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
              30, 31],
        '8': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
              30, 31],
        '9': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
              30, 31],
        '10': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
               29, 30, 31],
        '11': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
               29, 30, 31],
        '12': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
               29, 30, 31],

    }
    is_leap_month = False
    while month < 12:
        for day in days:
            ld = LunarDate(2020, month, day, 0) if is_leap_month is False else LunarDate(2020, month, day, 1)
            aDay = ld.to_solar_date()
            months_limitDays[str(aDay.month)].remove(aDay.day)
            print("农历：", month, "-", day, " 新历", aDay.month, "-", aDay.day)
        month += 1
        if month == 5 and is_leap_month is True:
            print("跑出闰月")
            is_leap_month = False
        elif month == 5 and is_leap_month is False:
            print("进入闰月")
            is_leap_month = True
            month -= 1
    for i in range(1, 6):
        months_limitDays.pop(str(i))
    for k, v in months_limitDays.items():
        print(k, v)


def run():
    spider = AIYZSpider()
    spider.setToken("")

    const = 200
    step = 5
    maxConst = 500
    while const < maxConst:
        const += step
        activity_name = "手推 %s元超市券" % const
        activity_id = spider.get_activity_id(activity_name, type=3)
        print("activity_id=", activity_id)
        if activity_id is None:
            payload = {"addActivity_payMethod": "5", "couponStartTime": "", "couponEndTime": "", "expiry": "2", "activeDay": "90",
             "delayDay": "0", "given": "1", "mutualUse": "0", "maxPerDay": "", "maxPerUser": "", "limitTimes": "",
             "limitDays": "", "limitWeeks": "", "memberLevel": "", "allowPayMethods": "1,2,4,5",
             "id": "", "name": activity_name, "type": "3", "amountType": "1", "minAmount": "",
             "maxAmount": "", "minArriveAmount": "", "maxArriveAmount": "", "giveOilType": "", "giveOilTypeNames": "",
             "needMember": "1", "startTime": "2020-06-21 00:00:00", "endTime": "2021-06-21 23:59:59",
             "oilTypeIds": "1267640638578081794", "oilTypeNames": "超市", "description": "", "limitDaysJoinTimes": "",
             "limitWeeksJoinTimes": "", "limitMonthJoinTimes": "", "limitJoinTimes": "", "token": spider.token}
            activity_id = spider.create_activity_by_payload(payload)

        template_name = "%s元超市券" % const
        type = 2  # 满减券

        templateId = spider.get__coupon_template_id(template_name, type)
        if spider.is_activity_has_coupon_template_id(activity_id, templateId) is False:
            if templateId is None:
                print("获取优惠券模板失败！")
            else:
                payload = {"id": "", "templateId": templateId, "couponCost": const,
                           "description": "超市购物使用满%s元可以使用！" % const, "quantity": "1",
                           "allowGiveAway": "", "maxActivePerUser": "", "activityId": activity_id,
                           "token": spider.token}
                spider.add_coupon_to_activity_by_payload(payload)
        if const >= maxConst:
            break;
        time.sleep(1)

#柴油9900返495元超市全额券 -50优惠券 9 张 - 45 优惠券 1 张
def jmjyz():
    spider = AIYZSpider()
    spider.setToken("")

    const = 5000
    step = 100
    per = 0.05
    maxConst = 10000
    while True:
        const += step
        activity_name = "柴油%s返%s元超市全额券" % (const, int(const * per))
        print(activity_name,'-50优惠券',int(const * per//50),'张 -',int((const * per)%50),'优惠券 1 张',)
        activity_id = spider.get_activity_id(activity_name, type=5)
        print("activity_id=", activity_id)
        if activity_id is None:
            payload =  {"addActivity_payMethod": "5", "couponStartTime": "", "couponEndTime": "", "expiry": "2", "activeDay": "60",
             "delayDay": "0", "given": "1", "mutualUse": "0", "maxPerDay": "", "maxPerUser": "", "limitTimes": "",
             "limitWeeks": "", "memberLevel": "", "allowPayMethods": "1,2,4,5", "allowGiftMethods": "1,2,3,4,5",
             "id": "", "name":activity_name, "type": "5", "amountType": "1",
             "allow_gift_methods": "5", "minAmount": const, "maxAmount": const + step - 0.01, "minArriveAmount": "",
             "maxArriveAmount": "", "giveOilType": "1250709561321488386", "giveOilTypeNames": "0#", "needMember": "0",
             "startTime": "2020-06-01 09:00:00", "endTime": "2021-05-26 23:59:59", "oilTypeIds": "1265170483226853377",
             "oilTypeNames": "超市", "description": "", "limitDaysJoinTimes": "", "limitWeeksJoinTimes": "",
             "limitMonthJoinTimes": "", "limitJoinTimes": "", "token": spider.token}
            activity_id = spider.create_activity_by_payload(payload)

        template_name = "50元超市券"
        type = 3  # 加油返券
        if int(const * per//50)>0:
            templateId = spider.get__coupon_template_id(template_name, type)
            if spider.is_activity_has_coupon_template_id(activity_id, templateId) is False:
                if templateId is None:
                    print("获取优惠券模板失败！")
                else:
                    payload = {"id": "", "templateId": templateId, "couponCost": "50",
                     "description": "代金券全额使用，烟酒不参与活动，有效期内抵用！", "quantity": int(const * per//50), "allowGiveAway": "", "maxActivePerUser": "",
                     "activityId": activity_id, "token": spider.token}
                    spider.add_coupon_to_activity_by_payload(payload)

        template_name = "%s元超市券" % int((const * per)%50)
        type = 3  # 加油返券

        if int((const * per)%50)>0:

            templateId = spider.get__coupon_template_id(template_name, type)
            if spider.is_activity_has_coupon_template_id(activity_id, templateId) is False:
                if templateId is None:
                    print("获取优惠券模板失败！")
                else:
                    payload = {"id": "", "templateId": templateId, "couponCost": int((const * per)%50),
                     "description": "代金券全额使用，烟酒不参与活动，有效期内抵用！", "quantity": "1", "allowGiveAway": "", "maxActivePerUser": "",
                     "activityId": activity_id, "token": spider.token}
                    spider.add_coupon_to_activity_by_payload(payload)


        if const >= maxConst:
            break;
        time.sleep(1)


if __name__ == '__main__':
    jmjyz()
